package com.wolfking.demo.nio.file;

import java.io.File;
import java.nio.MappedByteBuffer;
import java.nio.channels.FileChannel;
import java.nio.file.StandardOpenOption;

public class FileCopyMappedByteBuffer {
    public static void main(String[] args) throws Exception {
        File target = new File("H:\\ssl.txt");
        if (!target.exists()) {
            if (!target.getParentFile().exists()) {
                target.getParentFile().mkdirs();
            }
            target.createNewFile();
        }
        long start = System.currentTimeMillis();
        FileChannel readChannel = FileChannel.open(new File("C:\\ssl_key\\ssl.log").toPath(), StandardOpenOption.READ);
        MappedByteBuffer mappedByteBuffer = readChannel
                .map(FileChannel.MapMode.READ_ONLY, 0, readChannel.size());
        FileChannel writeChannel = FileChannel.open(target.toPath(), StandardOpenOption.WRITE);
        writeChannel.write(mappedByteBuffer);
        mappedByteBuffer.force();
        readChannel.close();
        writeChannel.close();
        System.out.println(System.currentTimeMillis() - start);
    }
}
